package pers.lan.stack;

import java.util.Stack;

/**
 * @author <p><a href="mailto:1728209643@qq.com">XiaoSan</a>
 * @date 2019/9/3 10:27 AM
 */
public class LC_921 {

    public static void main(String[] args) {
        System.out.println(
                new LC_921().minAddToMakeValid("()")
        );
    }

    public int minAddToMakeValid(String S) {
        Stack<Character> stack = new Stack<>();
        char[] chars = S.toCharArray();
        int sum = 0;
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == ')') {
                if (stack.empty()) {
                    sum++;
                } else {
                    stack.pop();
                }
            } else {
                stack.push('(');
            }
        }
        return sum + stack.size();
    }

}
